package com.xindz.cn.mvptest.shared.core.image;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.xindz.cn.mvptest.R;
import com.xindz.cn.mvptest.shared.core.glide.GlideRequest;
import com.xindz.cn.mvptest.shared.core.glide.GlideRequests;

/**
 * @name Base
 * @class name：com.xindz.cn.base.shared.core.image
 * @describe 图片加载工具类
 * @anthor XINDZ
 * @time 2017/10/26 13:22
 * @change
 * @chang time
 * @class describe
 */
public class ImageLoader {
    private static volatile ImageLoader imageLoader;

    private GlideRequests request;

    private ImageLoader() {
    }


    public static ImageLoader getInstance(@NonNull GlideRequests request) {
        if (imageLoader == null) {
            synchronized (ImageLoader.class) {
                if (imageLoader == null) {
                    imageLoader = new ImageLoader();
                }
            }
        }
        imageLoader.request = request;
        return imageLoader;
    }

    public GlideRequest defaultLoad(String imgUrl) {
        return request.load(imgUrl);
    }


    public GlideRequest crossLoadDrawable(String imgUrl) {
        RequestOptions requestOptions1 = new RequestOptions().fallback(R.drawable.icon_error);//空图片
        return request.load(imgUrl).apply(requestOptions1).transition(new DrawableTransitionOptions().crossFade(800));
    }

    /**
     * 加载BitMap图片
     *
     * @param imgUrl
     * @param bitmap
     * @return
     */
    public GlideRequest crossLoadBitmap(String imgUrl, Bitmap bitmap) {
        RequestOptions requestOptions1 = new RequestOptions().fallback(R.drawable.icon_error);//空图片
        //加载一个 Bitmap ，你需要使用 BitmapTransitionOptions
        return request.asBitmap().load(bitmap).apply(requestOptions1).transition(new BitmapTransitionOptions().crossFade(800));
    }

    /**
     * 加载圆形图片
     *
     * @param imgUrl
     * @return
     */
    public GlideRequest circleCropLoadDrawable(String imgUrl) {
        return request.load(imgUrl).circleCrop();
    }

    /**
     * 居中不拉伸
     *
     * @param imgUrl
     * @return
     */
    public GlideRequest centerCropLoadDrawable(String imgUrl) {
        return request.load(imgUrl).centerCrop();
    }

    /**
     * 拉伸
     *
     * @param imgUrl
     * @return
     */
    public GlideRequest fitLoadDrawable(String imgUrl) {
        return request.load(imgUrl).fitCenter();
    }

    /**
     * 拉伸
     *
     * @param imgUrl
     * @return
     */
    public GlideRequest fitLoadDrawablet(String imgUrl) {
        return request.load(imgUrl).fitCenter();
    }

    /**
     * 注意！！！该方法必须在子线程中执行
     * 清除硬盘缓存
     */
    public void cleanDiskCache(final Context context) {
        Glide.get(context).clearDiskCache();
    }

    /**
     * 清除内存缓存
     */
    public void cleanMemoryCache(Context context) {
        Glide.get(context).clearMemory();
    }

    /**
     * 内存和硬盘双清
     */
    public void cleanDoubleCache(Context context) {
        cleanDiskCache(context);
        cleanMemoryCache(context);
    }
}
